/*
 * IC Project - a prototype to test a hypothesis of collective intelligence
 * Copyright (C) 2012 Paolo Conte - paolo@concavi.net
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * This code is derived from the project ojoverse (2008, same author)
 * under the license GPL2. See <http://www.gnu.org/licenses/>.
 */

package org.ix.net;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ix.utils.ContextUtils;

public abstract class BaseServlet extends HttpServlet {

    protected ContextUtils utils = null;

    protected void baseprocess(HttpServletRequest req, HttpServletResponse res) {
        res.setContentType("text/html;charset=UTF-8");
        res.setHeader("Pragma","no-cache"); // HTTP 1.0
        res.setHeader("Cache-Control","no-cache"); //HTTP 1.1
        res.setDateHeader("Expires", 0); // proxy
        utils = new ContextUtils(getServletContext());

        process(req, res);
    }

    public String lbl(String key) {
        return utils.lbl(key);
    }

    public String prop(String key) {
        return utils.prop(key);
    }

    // convert null value into empty string
    public String nvc(String value) {
        return nvc(value, "");
    }

    public String nvc(String value, String replace) {
        return value == null ? replace : value;
    }

    protected abstract void process(HttpServletRequest request, HttpServletResponse response);

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        baseprocess(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        baseprocess(request, response);
    }

    public abstract String getServletInfo();
}
